﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.DataModel;
using Dict.Blo;
using ProtoBuf.Message;

namespace UI
{
    public class BreachPage : MonoBehaviour
    {
        #region Labels
        public List<UILabel> propertyBefore;    //顺序：level，att，hp，pdef，mdef，skill-1-2-3
        public List<UILabel> propertyAfter; //顺序：level，att，hp，pdef，mdef，skill-1-2-3
        public UILabel skillName;
        public UILabel breachTip;
        public UILabel progressValue;
        public UILabel breachMaterialHave;
        public UILabel breachMaterialNeed;
        #endregion

        public UISprite progreassBar;
        public GameObject skillPointer;
        public GameObject pointersRoot;
        public GameObject valuesAfterRoot;
        public GameObject progressParticleBig;
        public GameObject progressParticleSmall;
        public GameObject heroParticle;
        public GameObject starParticle;
        public GameObject progressBarRoot;

        string heroGuid;
        UIHero hero;
        int curLvCostItem;
        string costItemId;

        //进度条增长
        bool startGrow = false;
        int oldLevel = 0;
        int oldExp = 0;
        int expSpeed = 0;

        public void SetHero(string guid)
        {
            heroGuid = guid;
            hero = StaticData.playerData.InstPlayerData.GetUIHero(guid);
            UpdateAll();
        }

        void UpdateAll()
        {
            heroParticle.SetActive(false);
            progressParticleBig.SetActive(false);
            progressParticleSmall.SetActive(false);

            //本级数据
            TableHeroBreached curLevelData = DictHeroBreachBlo.GetTableHeroBreached(hero.breachlevel);
            propertyBefore[0].text = hero.breachlevel.ToString() + TableTextBlo.GetText("ji").Replace("\\n", "\n").Replace("\\r", "\r");
            propertyBefore[1].text = curLevelData.AttAddPercent.ToString() + "%";
            propertyBefore[2].text = curLevelData.HpAddPercent.ToString() + "%";
            propertyBefore[3].text = curLevelData.PDefAddPercent.ToString() + "%";
            propertyBefore[4].text = curLevelData.MDefAddPercent.ToString() + "%";

            bool haveBigSkill = false;
            for (int i = 0; i < hero.Skills.Length; i++)
            {
                if(hero.Skills[i] != null && hero.Skills[i].skillIndex == 2)
                {
                    haveBigSkill = true;
                    skillName.text = TableTextBlo.GetText("name_skill_" + hero.Skills[i].skillData.SkillId);
                    propertyBefore[5].text = hero.breachlevel.ToString() + TableTextBlo.GetText("ji");
                }
            }

            //下一级数据
            int nextLevel = hero.breachlevel + 1;
            TableHeroBreached nextData = DictHeroBreachBlo.GetTableHeroBreached(nextLevel);
            if(nextData == null)
            {
                //满级
                pointersRoot.SetActive(false);
                valuesAfterRoot.SetActive(false);
            }
            else
            {
                pointersRoot.SetActive(true);
                valuesAfterRoot.SetActive(true);

                propertyAfter[0].text = nextLevel.ToString() + TableTextBlo.GetText("ji").Replace("\\n", "\n").Replace("\\r", "\r");
                propertyAfter[1].text = (nextData.AttAddPercent).ToString() + "%";
                propertyAfter[2].text = (nextData.HpAddPercent).ToString() + "%";
                propertyAfter[3].text = (nextData.PDefAddPercent).ToString() + "%";
                propertyAfter[4].text = (nextData.MDefAddPercent).ToString() + "%";
                propertyAfter[5].text = nextLevel.ToString() + TableTextBlo.GetText("ji");
            }

            if(!haveBigSkill)
            {
                skillPointer.SetActive(false);
                skillName.text = "";
                propertyBefore[5].text = "";
                propertyAfter[5].text = "";
            }
            else
            {
                skillPointer.SetActive(true);
            }

            //经验条
            if (nextData == null)
            {
                //由于满级没有数据，所以用上一级数据
                curLevelData = DictHeroBreachBlo.GetTableHeroBreached(hero.breachlevel - 1);
            }
            
            string rateStr = "";
            if(hero.breachExp <= curLevelData.Critical1)
            {
                rateStr = "[f2f2f2]" + TableTextBlo.GetText("breach_rate_1") + "[-]";
            }
            else if (hero.breachExp <= curLevelData.Critical2)
            {
                rateStr = "[fffd33]" + TableTextBlo.GetText("breach_rate_2") + "[-]";
            }
            else
            {
                rateStr = "[ff3101]" + TableTextBlo.GetText("breach_rate_3") + "[-]";
            }
            string str = string.Format(TableTextBlo.GetText("break_through_desc"),rateStr);
            breachTip.text = str;

            if (nextData == null)
            {
                progressValue.text = curLevelData.Exp.ToString() + "/" + curLevelData.Exp.ToString();
                progreassBar.fillAmount = 1;
            }
            else
            {
                progressValue.text = hero.breachExp.ToString() + "/" + curLevelData.Exp.ToString();
                progreassBar.fillAmount = hero.breachExp / (float)curLevelData.Exp;
            }

            //突破水晶
            string[] costinfo = curLevelData.Cost.Split(',');
            costItemId = costinfo[0];
            long itemCount = StaticData.playerData.GetSimpleItemCount(costinfo[0]);
            breachMaterialHave.text = itemCount.ToString();
            breachMaterialNeed.text = costinfo[1];
            curLvCostItem = int.Parse(costinfo[1]);
        }

        public void OnBreachBtn()
        {
            if (startGrow)
                return;

            TableHeroBreached nextData = DictHeroBreachBlo.GetTableHeroBreached(hero.breachlevel + 1);
            if(nextData == null)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("breach_level_max"));
                return;
            }

            long itemCount = StaticData.playerData.GetSimpleItemCount(costItemId);
            if(itemCount < curLvCostItem)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("name_" + costItemId) + TableTextBlo.GetText("not_enough"));
                return;
            }
            CSWarriorBreached msg = new CSWarriorBreached();
            msg.guid = heroGuid;
            msg.type = 1;
            NetAPI.SendHttp(OpDefine.CSWarriorBreached, msg, BreachCallback);
        }

        void BreachCallback(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                oldLevel = hero.breachlevel;
                oldExp = hero.breachExp;
                SCWarriorBreached msg = packet.kBody as SCWarriorBreached;
                StaticData.playerData.Delete(msg.cost);
                StaticData.playerData.UpdateHero(msg.guid);

                int totalExpGrow = 0;
                TableHeroBreached data = DictHeroBreachBlo.GetTableHeroBreached(oldLevel);
                if(oldLevel < hero.breachlevel)
                {
                    totalExpGrow = (int)data.Exp - oldExp + hero.breachExp;
                }
                else
                {
                    totalExpGrow = hero.breachExp - oldExp;
                }

                expSpeed = totalExpGrow / 30;
                if (expSpeed <= 0)
                    expSpeed = 1;
                
                progressParticleSmall.SetActive(false);
                startGrow = true;
            }
        }

        void Update()
        {
            if (startGrow)
            {
                if(oldLevel < hero.breachlevel)
                {
                    oldExp += expSpeed;
                    TableHeroBreached data = DictHeroBreachBlo.GetTableHeroBreached(oldLevel);
                    if (oldExp > data.Exp)
                        oldExp = (int)data.Exp;

                    progressValue.text = oldExp.ToString() + "/" + data.Exp.ToString();
                    progreassBar.fillAmount = oldExp / (float)data.Exp;

                    progressParticleSmall.transform.localPosition = new Vector3((float)(progreassBar.width * (progreassBar.fillAmount - 0.5)), 0, 0);
                    progressParticleSmall.SetActive(true);

                    if (oldExp == data.Exp)
                    {
                        UpdateAll();

                        heroParticle.SetActive(true);
                        progressParticleBig.SetActive(true);
                        oldLevel++;
                        oldExp = 0;
                        AudioManager.Instance.PlayUIAudio("tupobaohua");
                        AudioManager.Instance.PlayUIAudio("tupojindutiaoFujia");
                        if (oldExp >= hero.breachExp)
                            startGrow = false;
                    }
                }
                else if(oldExp < hero.breachExp)
                {
                    oldExp += expSpeed;
                    TableHeroBreached data = DictHeroBreachBlo.GetTableHeroBreached(hero.breachlevel);

                    if (oldExp > hero.breachExp)
                        oldExp = hero.breachExp;

                    progressValue.text = oldExp.ToString() + "/" + data.Exp.ToString();
                    progreassBar.fillAmount = oldExp / (float)data.Exp;
                    
                    progressParticleSmall.transform.localPosition = new Vector3((float)(progreassBar.width * (progreassBar.fillAmount - 0.5)), 0, 0);
                    progressParticleSmall.SetActive(true);

                    if (oldExp == hero.breachExp)
                    {
                        startGrow = false;
                        UpdateAll();
                    }
                }
            }
        }

        public void OnBreachTen()
        {
            if (startGrow)
                return;

            TableHeroBreached nextData = DictHeroBreachBlo.GetTableHeroBreached(hero.breachlevel + 1);
            if (nextData == null)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("breach_level_max"));
                return;
            }

            long itemCount = StaticData.playerData.GetSimpleItemCount(costItemId);
            if (itemCount < curLvCostItem * 10)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("name_" + costItemId) + TableTextBlo.GetText("not_enough"));
                return;
            }

            CSWarriorBreached msg = new CSWarriorBreached();
            msg.guid = heroGuid;
            msg.type = 2;

            NetAPI.SendHttp(OpDefine.CSWarriorBreached, msg, BreachCallback);

        }

        public void SetParticleRenderOrder(bool up)
        {
            if(up)
            {
                progressBarRoot.GetComponent<UIPanel>().sortingOrder = 3;
                starParticle.SetActive(true);
            }
            else
            {
                progressBarRoot.GetComponent<UIPanel>().sortingOrder = 0;
                starParticle.SetActive(false);
            }
        }
    }
}

